package online.heycm.platform.canary.common;

import java.util.Arrays;
import lombok.Getter;

/**
 * 灰度策略
 *
 * @author heycm
 * @since 2023/12/30 11:29
 */
@Getter
public enum CanaryStrategy {

    /**
     * 根据特定请求头分发灰度
     */
    HEAD("X-CANARY-HEAD", "canary.head"),

    /**
     * 根据流量分发灰度
     */
    FLOW("X-CANARY-FLOW", "canary.flow");

    /**
     * 灰度请求携带请求头标记
     */
    private final String header;

    /**
     * 灰度服务元数据Key
     */
    private final String metadata;

    CanaryStrategy(String header, String metadata) {
        this.header = header;
        this.metadata = metadata;
    }

    public static CanaryStrategy ofNullable(String name) {
        return Arrays.stream(values()).filter(e -> e.name().equalsIgnoreCase(name)).findAny().orElse(null);
    }

    public static boolean contains(String name) {
        return ofNullable(name) != null;
    }

}
